import { getPicture, reqCheck } from '@/services/captcha';

const PointModel = {
  namespace: 'point',
  state: {
    secretKey: '', //后端返回的ase加密秘钥
    checkNum: 3, //默认需要点击的字数
    fontPos: [], //选中的坐标信息
    checkPosArr: [], //用户点击的坐标
    num: 1, //点击的记数
    pointBackImgBase: '', //后端获取到的背景图片
    poinTextList: [], //后端返回的点击字体顺序
    backToken: '', //后端返回的token值
    setSize: {
      imgHeight: 0,
      imgWidth: 0,
      barHeight: 0,
      barWidth: 0,
    },
    tempPoints: [],
    text: '',
    barAreaColor: undefined,
    barAreaBorderColor: undefined,
    showRefresh: true,
    bindingClick: true,
  },
  effects: {
    /**
     *
     * @param {payload} 中payload的结构{captchaType:"clickWord"}
     * @param {*} param1
     */
    *fetch({ payload }, { call, put }) {
      const response = yield call(getPicture, payload);
      yield put({
        type: 'getPicture',
        payload: response,
      });
    },
    /**
 *
 * @param
payload:

captchaType: "clickWord"
pointJson: "eLxQqh36JQ5NpX2bLhnJdQLpJ0e/YTPuTwGV3C5FgHZ5YTOBdilBbasjnLW4qETgUEbNypvdyEIG/GYW9N0T7w=="
token: "50f625e6677d49678d0bc06d8f7b764f"
 * @param {*} param1
 */
    *check({ payload }, { call, put }) {
      const response = yield call(reqCheck, payload);
      yield put({
        type: 'checkPicture',
        payload: response,
      });
    },
  },
  reducers: {
    getPicture(state, action) {
      const { payload } = action;
      const { repData } = payload;
      if (payload.repCode == '0000') {
        return {
          ...state,
          backImgBase: repData.originalImageBase64,
          blockBackImgBase: repData.jigsawImageBase64,
          backToken: repData.token,
          secretKey: repData.secretKey,
        };
      } else {
        return { ...state, text: payload.repMsg };
      }
    },

    checkPicture(state, action) {
      const { payload } = action;
      const successInfo = {
        barAreaColor: '#4cae4c',
        barAreaBorderColor: '#5cb85c',
        text: '验证成功',
        bindingClick: false,
      };
      const errorInfo = {
        barAreaColor: '#d9534f',
        barAreaBorderColor: '#d9534f',
        text: '验证失败',
      };
      if (payload.repCode == '0000') {
        return { ...state, ...successInfo };
      } else {
        return { ...state, ...errorInfo };
      }
    },
  },
};

export default PointModel;
